﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using uk.ac.soton.ses.DAL;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI;

namespace uk.ac.soton.ses
{
    public static class HDCSharedWebFunctions
    {

        public static MaterialsMetadataContext GetSharedDBContext()
        {
            return HttpContext.Current.GetMaterialsDBContext();
        }

        /// <summary>
        /// Add a new label containing a message
        /// </summary>
        /// <param name="message">The message to print</param>
        /// <param name="container">The container to add the label to</param>
        /// <returns>The Label Control object</returns>
        public static Label AddErrorLabel(string message, Control container, int addAtIndex = -1)
        {
            if (addAtIndex < 0)
            {
                addAtIndex = container.Controls.Count;
            }

            Label errorLabel = new Label();
            errorLabel.Text = message;
            errorLabel.CssClass = "ErrorLabel";
            container.Controls.AddAt(addAtIndex, errorLabel);
            return errorLabel;
        }

        /// <summary>
        /// Add a new label containing a message
        /// </summary>
        /// <param name="message">The message to print</param>
        /// <param name="container">The container to add the label to</param>
        /// <returns>The Label Control object</returns>
        public static Label AddMessageLabel(string message, Control container, int addAtIndex = -1)
        {
            if (addAtIndex < 0)
            {
                addAtIndex = container.Controls.Count;
            }

            Label messageLabel = new Label();
            messageLabel.Text = message;
            messageLabel.CssClass = "MessageLabel";
            container.Controls.AddAt(addAtIndex, messageLabel);
            return messageLabel;
        }

        public static HyperLink AddMessageHyperLink(string message, string navigateUrl, Control container, string target = "_blank", int addAtIndex = -1)
        {
            if (addAtIndex < 0)
            {
                addAtIndex = container.Controls.Count;
            }

            HyperLink newlink = new HyperLink();
            newlink.Text = message;
            newlink.NavigateUrl = navigateUrl;
            newlink.Target = target;
            container.Controls.AddAt(addAtIndex, newlink);
            return newlink;
        }
    }
}
